package wwcontrolserver.sensor;

///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package wwcontrolserver;
//
////import java.lang.Math;
///**
// *
// * @author Michael Ferry
// */
public class SensorComputationsServer {
//    //private static int pHORP_serial=174418;
//    //private static int HeaterSensor_serial=106721;
//    
//    public static double ComputeSensorValue(int SerialNumber,int SensorNumber, int SensorRawValue, double MAX){
//        //System.out.println(SerialNumber);
//        if(SerialNumber == WWControlServer.ifk_pHORP_serial){
//            //sensors attached to this device (positions 0-7)
//            //Sensor Input 1: Phidgets 1130 pH/ORP adapter - set to pH
//            //Sensor Input 2: Phidgets 1130 ORP adapter - set to ORP
//            //Sensor Input 3: Phidgets 1124 Precision temperature sensor
//            if(SensorNumber == 0){
//                return ComputeORPmV(SensorRawValue, MAX);
//            }else if(SensorNumber == 1){
//                return ComputePHmV(SensorRawValue, MAX);            
//            }else if(SensorNumber == 3){
//                return Compute1124Temp(SensorRawValue , MAX);
//            }    
//        }else if(SerialNumber == WWControlServer.ifk_HeaterSensor_serial){
//            if(SensorNumber == 0 || SensorNumber == 1 || SensorNumber == 2 || SensorNumber == 3){
//                return ComputeTemperature2252ohms(SensorRawValue, MAX);
//            }
//                
//        }else if(SerialNumber == WWControlServer.ifk_P3_serial){
//            if(SensorNumber == 0) return ComputeORPmV(SensorRawValue, MAX);
//            else if(SensorNumber == 1) return ComputePHmV(SensorRawValue, MAX);
//        }else if(SerialNumber == WWControlServer.ifk_P4_serial){
//            if(SensorNumber == 1) return ComputeORPmV(SensorRawValue, MAX);
//            else if(SensorNumber == 0) return ComputePHmV(SensorRawValue, MAX);
//        }
//        
//        return SensorRawValue;
//    }
//          
    public static double ComputePHmV(int SensorRawValue, double MAX){
            //sensor ranges from -400mV to 400mV
            //return 0.8008*(SensorRawValue/4.095) - 400.8;
            return 0.0178*(SensorRawValue/MAX) - 1.889;
    }
    public static double ComputeORPmV(int SensorRawValue, double MAX){
            //sensor ranges from -2000mV to 2000mV
            return  1000*(2.5 - (SensorRawValue/MAX)/200)/1.037;
    }
    public static double Compute1124Temp(int SensorRawValue, double MAX){
        return (SensorRawValue/MAX)*0.222 - 61.111;
    }
    
    public static double Compute1134AttachedResistance(double SetResistance1134, int SensorRawValue, double MAX){
        return SetResistance1134*(((1000*MAX)/SensorRawValue)-1);
    }
    public static double ComputeThermistorTemperature(int SensorResistanceAt25, 
            double SetResistance1134,int SensorRawValue, double MAX){
        //Determine the current resistance of the thermistor;
        double SensorResistance = Compute1134AttachedResistance(SetResistance1134,SensorRawValue, MAX);
        //Convert this resistance to a temperature reading
        return 25 - Math.log(SensorResistance/SensorResistanceAt25)/Math.log(1.04);
    }
            
    public static double ComputeTemperature2252ohms(int SensorRawValue, double MAX) {
        //double PhidgetsRawSensorResolution=4096;
        double Temperature;
        double PhidgetsVoltDividerResistance = 2610;
        int SensorResistanceAt25 = 2252;
        
        double SensorResistance =  PhidgetsVoltDividerResistance*(((1000*MAX)/SensorRawValue)-1);
        
        Temperature = 25 - Math.log(SensorResistance/SensorResistanceAt25)/Math.log(1.04);
         
         //System.out.println("Raw: " + SensorRawValue + "SR: " + SensorResistance+ "Temp: "+ Temperature);
        return Temperature;
    }
//    
}
